package org.grayrabbit.cms.publish.generate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.grayrabbit.cms.publish.api.Publishable;



public class SiteMission extends Mission{
	private int currentSiteIndex;
	private int currentCatIndex;
	private int currentCatListIndex;
	private int currentTopicIndex;
	private int currentTopicListIndex;
	private int currentContentIndex;
	private int currentSurveryIndex;
	private static List<String> EMPTY_LIST = Collections.emptyList();
	private int current = 0;
	
	public int getCurrentTopicIndex() {
		return currentTopicIndex;
	}

	public void setCurrentTopicIndex(int currentTopicIndex) {
		this.currentTopicIndex = currentTopicIndex;
	}

	public int getCurrentTopicListIndex() {
		return currentTopicListIndex;
	}

	public void setCurrentTopicListIndex(int currentTopicListIndex) {
		this.currentTopicListIndex = currentTopicListIndex;
	}

	public int getCurrentSiteIndex() {
		return currentSiteIndex;
	}

	public void setCurrentSiteIndex(int currentSiteIndex) {
		this.currentSiteIndex = currentSiteIndex;
	}

	public int getCurrentCatIndex() {
		return currentCatIndex;
	}
	public void setCurrentCatIndex(int currentCatIndex) {
		this.currentCatIndex = currentCatIndex;
	}
	public int getCurrentCatListIndex() {
		return currentCatListIndex;
	}
	public void setCurrentCatListIndex(int currentCatListIndex) {
		this.currentCatListIndex = currentCatListIndex;
	}
	public int getCurrentContentIndex() {
		return currentContentIndex;
	}

	public void setCurrentContentIndex(int currentContentIndex) {
		this.currentContentIndex = currentContentIndex;
	}

	public int getCurrentSurveryIndex() {
		return currentSurveryIndex;
	}

	public void setCurrentSurveryIndex(int currentSurveryIndex) {
		this.currentSurveryIndex = currentSurveryIndex;
	}
	

	@Override
	public GenTask next() {
		if(currentSiteIndex<this.getIndexNum()){
			GenTask task = new GenTask();
			task.setType(Publishable.INDEXT_GEN);
			task.setMission(this);
			task.setAdmin(this.getAdmin());
			if((getIndexNum()-currentSiteIndex)>MAX){
				List<String> list = new ArrayList<String>(this.getList().subList(currentSiteIndex, currentSiteIndex+MAX));
				task.setList(list);
				currentSiteIndex += MAX;
				return task;
			}else{
				List<String> list = new ArrayList<String>(this.getList().subList(currentSiteIndex,this.getList().size()));
				task.setList(list);
				currentSiteIndex = this.getList().size();
				return task;
			}
		}else if(currentCatIndex<this.getCatNum()/2){
			List<String> catList = generatable.findCatIdsBySiteId(getList(),currentCatIndex, MAX);
			GenTask task = new GenTask();
			task.setList(catList);
			task.setType(Publishable.CATEGORY_GEN);
			task.setMission(this);
			task.setAdmin(this.getAdmin());
			if(catList.size()==0){
				currentCatIndex = this.getCatNum()/2;
			}else{
				currentCatIndex+=catList.size();
			}
			return task;
		}else if(currentCatListIndex<this.getCatNum()/2){
			List<String> catList = generatable.findCatIdsBySiteId(getList(),currentCatListIndex, MAX);
			GenTask task = new GenTask();
			task.setList(catList);
			task.setType(Publishable.CATEGORY_LIST_GEN);
			task.setMission(this);
			task.setAdmin(this.getAdmin());
			if(catList.size()==0){
				currentCatListIndex = this.getCatNum()/2;
			}else{
				currentCatListIndex+=catList.size();
			}
			return task;
		}else if(currentTopicIndex<this.getTopicNum()/2){
			List<String> topicList = generatable.findTopicIdsBySiteId(getList(),currentTopicIndex, MAX);
			GenTask task = new GenTask();
			task.setList(topicList);
			task.setType(Publishable.TOPIC_GEN);
			task.setMission(this);
			task.setAdmin(this.getAdmin());
			if(topicList.size()==0){
				currentTopicIndex = this.getTopicNum()/2;
			}else{
				currentTopicIndex+=topicList.size();
			}
			return task;
		}else if(currentTopicListIndex<this.getTopicNum()/2){
			List<String> topicList = generatable.findTopicIdsBySiteId(getList(),currentTopicListIndex, MAX);
			GenTask task = new GenTask();
			task.setList(topicList);
			task.setType(Publishable.TOPIC_LIST_GEN);
			task.setMission(this);
			task.setAdmin(getAdmin());
			if(topicList.size()==0){
				currentTopicListIndex = this.getTopicNum()/2;
			}else{
				currentTopicListIndex+=topicList.size();
			}
			return task;
		}else if(currentContentIndex<this.getContentNum()){
			List<String> contentIds = null;
			if(Publishable.ALL_PUBLISH.equalsIgnoreCase(this.getModelType())){
				contentIds = generatable.findContentIdsBySiteId(getList(),current,MAX);
			}else{
				contentIds = generatable.findContentIdsBySiteId(getList(),current,MAX);
			}
			if(contentIds.size()>0){
				current = Integer.parseInt(contentIds.get(0));//第一位为当前偏移量
			}
			if(contentIds.size()>1){
				currentContentIndex += (contentIds.size()-1);
				return createContentTask(contentIds.subList(1, contentIds.size()));
			}else{
				if(currentContentIndex<getContentNum()){
					//此处可能因为内空有变动，导致等发布内容数变化，进行微调处理，以保证发布任务能正常结束。
					currentContentIndex = getContentNum();
				}
			}
			return createContentTask(EMPTY_LIST);//空处理，减少空指针异常
		}else if(currentSurveryIndex<this.getSurveryNum()){
			List<String> surveyList = generatable.findSurveyIdsBySiteId(getList(),currentSurveryIndex, MAX);
			GenTask task = new GenTask();
			task.setList(surveyList);
			task.setType(Publishable.SURVEY_GEN);
			task.setAdmin(getAdmin());
			task.setMission(this);
			if(surveyList.size()==0){
				currentSurveryIndex = this.getSurveryNum();
			}else{
				currentSurveryIndex+=surveyList.size();
			}
			return task;
		}
		return null;
	}
	
	private GenTask createContentTask(List<String> contentList){
		GenTask task = new GenTask();
		task.setList(contentList);
		task.setType(Publishable.CONTENT_GEN);
		task.setMission(this);
		task.setAdmin(getAdmin());
		return task;
	}

	public boolean hasNext() {
		return currentSiteIndex<this.getIndexNum()
				||currentCatIndex<this.getCatNum()/2
				||currentCatListIndex<this.getCatNum()/2
				||currentTopicIndex<this.getTopicNum()/2
				||currentContentIndex<this.getContentNum()
				||currentSurveryIndex<this.getSurveryNum();
	}

	public void remove() {
	}
}
